package yxy.cherry.paycenter.channelmodule;

import com.alibaba.fastjson.JSONObject;
import org.joda.time.DateTime;
import yxy.apple.util.ArraryMesh;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.*;
import yxy.cherry.paycenter.database.dao.ordersDao;
import yxy.cherry.paycenter.database.db.orders;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.module.PayInfoModule;
import yxy.game.pm2.bean.order.PayInfo;
import yxy.game.pm2.module.player.FunctionHandle;

import java.text.SimpleDateFormat;
import java.util.*;

public abstract class PayMoudle_channel {

//	public static final int finish = 1;	//订单已完成
//	public static final int runing = 2;	//订单进行中

    /**
     * 处理渠道支付回调
     *
     * @param infoobj
     * @return
     */
    abstract public JSONObject dealPay(JSONObject infoobj);

    /**
     * 创建渠道支付订单信息   预订单
     *
     * @param infoobj 补充
     * @param o
     * @return
     */
    abstract public JSONObject createOrderInfo(JSONObject infoobj, orders o) throws Exception;

    abstract public String getChannel();

    /**
     * 获取奖励信息
     *
     * @param pcode  玩家code
     * @param type   充值类型
     * @param dataid 充值id
     * @return
     */
    public ArrayList<Currency> getRewardInfoByType(String pcode, ActType type, int dataid) {
        ArrayList<Currency> curs = new ArrayList<Currency>();

//		charge c1 = DataCenter.getData(dataid, charge.class);
//		chargepake cp1 = DataCenter.getData(dataid, chargepake.class);

        //todo 相隔了几个月(动态增加商品数量 2022.08.26)
        Integer passMonth = null;
        Integer maxraisetimes = null;
        PlayerBean playerBean = null;
        Integer begin_server = null;
        try {
            passMonth = FunctionHandle.getAlreadlyPassMonth(pcode);
            config data = DataCenter.getData(4, config.class);
            JSONObject jsonObject = JSONObject.parseObject(data.getValue());
            maxraisetimes = jsonObject.getInteger("maxraisetimes");
            playerBean = PlayerBean.get(pcode);
            begin_server = jsonObject.getInteger("begin_server");
        } catch (Exception e) {
            e.printStackTrace();
        }


        switch (type) {
            case 普通充值: {
                charge c = DataCenter.getData(dataid, charge.class);
                int num = c.getDiamond() + c.getGive();

                PayInfo pi = PayInfoModule.get(pcode);
                if (pi != null) {
                    if (!pi.isHadfirstpay()) {
                        num = num + c.getFrgive();
                    }
                }

                Currency cur = Currency.create(CurrencyType.CT_元宝, -1, num);
                curs.add(cur);
                break;
            }
            case 每日首充: {
                break;
            }
            case 每日礼包: {
                chargepake cp = DataCenter.getData(dataid, chargepake.class);

                int[] rewardtypes = cp.getRewardtype();
                int[] rewardids = cp.getRewardid();
                int[] rewardnums = cp.getRewardnum();

                curs = (ArrayList<Currency>) Currency.create(rewardtypes, rewardids, rewardnums);
                break;
            }
            case 特权商城: {
                //TODO:        特权订单奖励处理
                Map<String, chargeshop> css = DataCenter.getAllData(chargeshop.class);
                for (String key : css.keySet()) {
                    chargeshop cs = css.get(key);
                    if (cs.getPurchasetype() != 1) {
                        continue;
                    }
                    if (cs.getPurchasever() == dataid) {
                        curs = (ArrayList<Currency>) Currency.create(cs.getRewardtype(), cs.getRewardid(), cs.getRewardnum());
                    }
                }
                break;
            }
            case 每周特惠: {
                actweekdiscount awc = ActDataCenter.getData(dataid, actweekdiscount.class, pcode);

                int[] rewardtypes = awc.getRewardtype();
                int[] rewardids = awc.getRewardid();
                int[] rewardnums = awc.getRewardnum();

                curs = (ArrayList<Currency>) Currency.create(rewardtypes, rewardids, rewardnums);
                break;
            }
            case 每月特惠: {
                actmondiscount amc = ActDataCenter.getData(dataid, actmondiscount.class, pcode);

                int[] rewardtypes = amc.getRewardtype();
                int[] rewardids = amc.getRewardid();
                int[] rewardnums = amc.getRewardnum();

                curs = (ArrayList<Currency>) Currency.create(rewardtypes, rewardids, rewardnums);
                break;
            }
            case 成长基金: {
                return curs;
            }
            case 限时抢购: {
                Map<String, actsnapup> asmap = ActDataCenter.getAllData(actsnapup.class, pcode);
                for (String key : asmap.keySet()) {
                    actsnapup as = asmap.get(key);
                    if (as.getPurchasever() != dataid) {
                        continue;
                    }
                    curs = (ArrayList<Currency>) Currency.create(as.getRewardtype(), as.getRewardid(), as.getRewardnum());
                    return curs;
                }
                break;
            }
            case 养成礼包: {
                Map<String, actcultivate> actmap = ActDataCenter.getAllData(actcultivate.class, pcode);
                for (String key : actmap.keySet()) {
                    actcultivate act = actmap.get(key);
                    if (act.getPurchasever() != dataid) {
                        continue;
                    }
                    curs = (ArrayList<Currency>) Currency.create(act.getRewardtype(), act.getRewardid(), act.getRewardnum());
                    return curs;
                }
                break;
            }
            case 限时礼包: {
                Map<String, actlimitpackage> alpmap = ActDataCenter.getAllData(actlimitpackage.class, pcode);
                for (actlimitpackage execl : alpmap.values()) {
                    if (execl.getPurchasever() != dataid) {
                        continue;
                    }
                    curs = (ArrayList<Currency>) Currency.create(execl.getRewardtype(), execl.getRewardid(), execl.getRewardnum());
                    return curs;
                }
                break;
            }
            case 每日特惠: {
                Map<String, acttimelimit> map = ActDataCenter.getAllData(acttimelimit.class, pcode);
                for (acttimelimit execl : map.values()) {
                    if (execl.getChargeid() != dataid) {
                        continue;
                    }
                    curs = (ArrayList<Currency>) Currency.create(execl.getRewardtype(), execl.getRewardid(), execl.getRewardnum());
                    return curs;
                }
                break;
            }
            case 关卡基金: {
                actfundrewarddate execl = ActDataCenter.getData(dataid, actfundrewarddate.class, pcode);
                if (execl == null) {
                    return null;
                }
                return (ArrayList<Currency>) Currency.create(execl.getRewardtype(), execl.getRewardid(), execl.getRewardnum());
            }
            case 超值月基金: {
                Map<String, actmonthfunddate> execlmap = ActDataCenter.getAllData(actmonthfunddate.class, pcode);
                for (actmonthfunddate execl : execlmap.values()) {
                    if (execl.getChargeid() == dataid) {
                        return (ArrayList<Currency>) Currency.create(execl.getRewardtype(), execl.getRewardid(), execl.getRewardnum());
                    }
                }
            }
            case 周卡: {
                Map<String, actweekcark> awcmap = ActDataCenter.getAllData(actweekcark.class, pcode);
                for (actweekcark data : awcmap.values()) {
                    if (data.getChargeid() == dataid) {
                        return (ArrayList<Currency>) Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
                    }
                }

                Map<String, actweekcarkshop> awcsmap = ActDataCenter.getAllData(actweekcarkshop.class, pcode);
                for (actweekcarkshop data : awcsmap.values()) {
                    if (data.getChargeid() == dataid) {
                        int[] num = playerBean.getServerId() >= begin_server ? ArraryMesh.getNum(data.getShopnum(), data.getRaise(), passMonth, maxraisetimes) : data.getShopnum();
                        return (ArrayList<Currency>) Currency.create(data.getShoptype(), data.getShopid(), num);
                    }
                }
                break;
            }
            case 神魔卡: {
                Map<String, actghostshop> agsmap = ActDataCenter.getAllData(actghostshop.class, pcode);
                for (actghostshop data : agsmap.values()) {
                    if (data.getChargeid() == dataid) {
                        int[] num = playerBean.getServerId() >= begin_server ? ArraryMesh.getNum(data.getShopnum(), data.getRaise(), passMonth, maxraisetimes) : data.getShopnum();
                        return (ArrayList<Currency>) Currency.create(data.getShoptype(), data.getShopid(), num);
                    }
                }
                break;
            }
            case 翻牌: {
                Map<String, actflopshop> afsmap = ActDataCenter.getAllData(actflopshop.class, pcode);
                for (actflopshop data : afsmap.values()) {
                    if (data.getChargeid() == dataid) {
                        int[] num = playerBean.getServerId() >= begin_server ? ArraryMesh.getNum(data.getShopnum(), data.getRaise(), passMonth, maxraisetimes) : data.getShopnum();
                        return (ArrayList<Currency>) Currency.create(data.getShoptype(), data.getShopid(), num);
                    }
                }
                break;
            }
            case 支付等级礼包: {
                Map<String, actgraderecharge> execlmap = ActDataCenter.getAllData(actgraderecharge.class, pcode);
                for (actgraderecharge data : execlmap.values()) {
                    if (data.getChargeid() == dataid) {
                        return (ArrayList<Currency>) Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
                    }
                }
                break;
            }
            case 关羽招募: {
                Map<String, actrecruitcharge> execlmap = ActDataCenter.getAllData(actrecruitcharge.class, pcode);
                for (actrecruitcharge data : execlmap.values()) {
                    if (data.getPurchasever() == dataid) {
                        return (ArrayList<Currency>) Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
                    }
                }
                break;
            }
            case 节日活动: {
                Map<String, atcholidayshop2> execlmap = DataCenter.getAllData(atcholidayshop2.class);
                for (atcholidayshop2 data : execlmap.values()) {
                    if (data.getPurchasever() == dataid) {
                        int[] num = playerBean.getServerId() >= begin_server ? ArraryMesh.getNum(data.getRewardnum(), data.getRaise(), passMonth, maxraisetimes) : data.getRewardnum();
                        return (ArrayList<Currency>) Currency.create(data.getRewardtype(), data.getRewardid(), num);
                    }
                }
                break;
            }
            case 招募令礼包: {
                Map<String, actgiftdata> actmap = ActDataCenter.getAllData(actgiftdata.class, pcode);
                for (String key : actmap.keySet()) {
                    actgiftdata act = actmap.get(key);
                    if (act.getPurchasever() != dataid) {
                        continue;
                    }
                    curs = (ArrayList<Currency>) Currency.create(act.getRewardtype(), act.getRewardid(), act.getRewardnum());
                    return curs;
                }
                break;
            }
            case 武将直升礼包: {
                Map<String, actstartupdata> actmap = ActDataCenter.getAllData(actstartupdata.class, pcode);
                for (String key : actmap.keySet()) {
                    actstartupdata act = actmap.get(key);
                    if (act.getChargeid() != dataid) {
                        continue;
                    }
                    curs = (ArrayList<Currency>) Currency.create(act.getRewardtype(), act.getRewardid(), act.getRewardnum());
                    return curs;
                }
                break;
            }
            case 心仪武将: {
                Map<String, actfavoritewarlordshop> map = ActDataCenter.getAllData(actfavoritewarlordshop.class, pcode);
                for (String key : map.keySet()) {
                    actfavoritewarlordshop shop = map.get(key);
                    if (shop.getChargeid() != dataid) {
                        continue;
                    }
                    curs = (ArrayList<Currency>) Currency.create(shop.getItemtype(), shop.getItemid(), shop.getItemnum());
                    return curs;
                }
                break;
            }

            case 心愿瓶: {
                Map<String, actwishshop> map = DataCenter.getAllData(actwishshop.class);
                for (String key : map.keySet()) {
                    actwishshop shop = map.get(key);
                    if (shop.getPurchasever() != dataid) {
                        continue;
                    }
                    curs = (ArrayList<Currency>) Currency.create(shop.getRewardtype(), shop.getRewardid(), shop.getRewardnum());
                    return curs;
                }
                break;
            }

            case 国庆寻宝: {
                Map<String, acttreasureshop> map = DataCenter.getAllData(acttreasureshop.class);
                for (String key : map.keySet()) {
                    acttreasureshop shop = map.get(key);
                    if (shop.getPurchasever() != dataid) {
                        continue;
                    }
                    curs = (ArrayList<Currency>) Currency.create(shop.getRewardtype(), shop.getRewardid(), shop.getRewardnum());
                    return curs;
                }
                break;
            }
        }
        return curs;
    }


    public boolean addOrder(orders o) {
        return ordersDao.add(o);
    }

    public boolean update(orders o) {
        return ordersDao.update(o);
    }

    public static String addExt(String ext, String key, String value) {
        StringBuilder sb = new StringBuilder(ext);
        sb.append(key).append(":").append(value).append(",");
        return sb.toString();
    }

    /**
     * 拼接透传参数
     *
     * @return
     */
    public static String createExt(JSONObject json) {
        StringBuilder sb = new StringBuilder();
        Set<String> keys = json.keySet();
        for (String key : keys) {
            String value = json.getString(key);
            sb.append(key).append(":").append(value).append(",");
        }
        return sb.toString();
    }

    /**
     * 解析透传参数
     *
     * @param str
     * @return
     */
    public static JSONObject analysisExt(String str) {
        JSONObject json = new JSONObject();
        String[] arr1 = str.split(",");
        for (int i = 0; i < arr1.length; i++) {
            String sss = arr1[i];
            if (sss == null || sss.equals("")) {
                continue;
            }

            String[] arr2 = sss.split(":");
            String key = arr2[0];
            String value = arr2[1];
            json.put(key, value);
        }
        return json;
    }

    /**
     * order实例
     *
     * @param channel
     * @param dataid
     * @param playercode
     * @param serverid
     * @return
     */
    public static orders createOrder(String channel, int dataid, int acttype, String playercode, int serverid) {
        charge c = DataCenter.getData(dataid, charge.class);
        DateTime dt = DateTime.now();

        orders o = new orders();
        o.setChannel(channel);
        o.setCreatetime(dt.toDate());
        o.setGoodsid(dataid);
        o.setGoodsname(c.getName());
        o.setGoodsprice(c.getMoney());
        o.setPayamount(0);
        o.setMyorderid(getOrderId());
        o.setOrderstate(0);
        o.setActtype(acttype);
        o.setPlatorderid(null);
        o.setPlayercode(playercode);
        o.setServerid(serverid);
        o.setUpdatetime(dt.toDate());

        //专服
        ServerBean serverBean = ServerBean.get(serverid);
        AreaBean areaBean = AreaBean.get(serverBean.getAreaId());
        o.setClusterid(areaBean.getClusterId());
        return o;
    }

    /**
     * 获取订单号
     *
     * @return
     */
    public static String getOrderId() {
        String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
        String seconds = new SimpleDateFormat("HHmmss").format(new Date());
        String orderid = date + getRandomOfSize(3) + seconds + getRandomOfSize(3);
        return orderid;
    }

    /**
     * 产生几位随机数
     *
     * @return
     */
    public static String getRandomOfSize(int size) {
        Random rad = new Random();
        int limits = (int) Math.pow(10, size);
        String result = rad.nextInt(limits) + "";
        if (result.length() < size) {
            String adds = "";
            for (int i = 0; i < size - result.length(); i++) {
                adds = adds + "0";
            }
            result = adds + result;
        }
        return result;
    }

}
